package com.dzl.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dzl.api.pojo.dto.ExtractCardDto;
import com.dzl.api.service.ExtractCardService;
import com.dzl.api.util.OkHttpUtil;
import com.dzl.constant.ApiExtractCardConstant;
import com.dzl.operation.service.OperationLogService;
import com.dzl.order.pojo.WelfareOrder;
import com.dzl.order.pojo.WelfareOrderApiCard;
import com.dzl.order.service.WelfareOrderApiCardService;
import com.dzl.order.service.WelfareOrderService;
import com.dzl.util.DateUtil;
import com.dzl.util.EmptyUtils;
import com.dzl.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ChenLong
 * @date 2021-11-10 11:53:02
 */
@Slf4j
@Service
public class ExtractCardServiceImpl implements ExtractCardService {

    @Autowired
    private OperationLogService operationLogService;
    @Autowired
    private WelfareOrderService welfareOrderService;
    @Autowired
    private WelfareOrderApiCardService welfareOrderApiCardService;

    @Override
    public String extractCard(String productId, Integer orderId,String phone,Integer quantity) {
        String sign = quantity + orderId.toString() + ApiExtractCardConstant.MERCHANT_ID + ApiExtractCardConstant.NOTIFY_URL + productId + ApiExtractCardConstant.KEY;
        log.info("extractCard sign: " + sign);
        String data = "merchantId=" + ApiExtractCardConstant.getMerchantId()
                + "&extOrderId=" + orderId
                + "&productId=" + productId
                + "&phone=" + phone
                + "&buyNumber=" + quantity
                + "&sign=" + MD5Util.MD5Encode(sign,null)
                + "&notifyUrl=" + ApiExtractCardConstant.NOTIFY_URL;
        log.info("extractCard data: " + data);
        operationLogService.saveOrderReturnSuccess(orderId, "extractCard data: " + data);
        // 下单请求
        String result = OkHttpUtil.sendPostWithHeads(ApiExtractCardConstant.DIRECT_URL, data,"application/x-www-form-urlencoded; charset=UTF-8",null);
        log.info("extractCard result: " + result);
        operationLogService.saveOrderReturnSuccess(orderId, "extractCard result: " + result);
        return result;
    }

    @Override
    public Integer updateExtractCard(ExtractCardDto dto) {
        WelfareOrder welfareOrderUpd = new WelfareOrder();
        welfareOrderUpd.setId(dto.getExtOrderId());
        if (dto.getState().equals(4)){
            welfareOrderUpd.setApiStatus(2);
        } else {
            welfareOrderUpd.setApiStatus(3);
        }
        welfareOrderUpd.setApiOrderNo(dto.getOrderId());
        welfareOrderService.updateByPrimaryKeySelective(welfareOrderUpd);
        String cardInfoStr = dto.getCardInfo();
        log.info("cardInfoStr :" + cardInfoStr);
        JSONArray jsonArray = JSONArray.parseArray(cardInfoStr);
        log.info("jsonArray :" + jsonArray);

//        cardInfoStr = cardInfoStr.replaceAll("\\\\","");
//        cardInfoStr = cardInfoStr.substring(1,cardInfoStr.length()-1);
//        log.info("updateExtractCard cardInfoStr: " + cardInfoStr);
//        JSONArray jsonArray = JSONArray.parseArray(cardInfoStr);
        if (EmptyUtils.isNotEmpty(jsonArray) && jsonArray.size() > 0) {
            List<WelfareOrderApiCard> list = new ArrayList<>();
            for (int i=0;i<jsonArray.size();i++){
                JSONObject obj = (JSONObject) jsonArray.get(i);
                log.info("updateExtractCard obj: " + obj.toString());
                WelfareOrderApiCard welfareOrderApiCardOpt = new WelfareOrderApiCard();
                welfareOrderApiCardOpt.setOrderId(dto.getExtOrderId());
                welfareOrderApiCardOpt.setCreateTime(new Date());
                welfareOrderApiCardOpt.setStatus(1);
                if (EmptyUtils.isNotEmpty(obj.get("cardNo"))){
                    welfareOrderApiCardOpt.setCardNo(obj.get("cardNo").toString());
                }
                if (EmptyUtils.isNotEmpty(obj.get("cardPwd"))){
                    welfareOrderApiCardOpt.setCardPwd(obj.get("cardPwd").toString());
                }
                if (EmptyUtils.isNotEmpty(obj.get("expireTime"))){
                    //Date expDate = TimeUtils.timeStampToDate(obj.get("expireTime").toString(),"yyyy-MM-DD HH:mm:ss");
                    Date expDate = DateUtil.parseDateTime(obj.get("expireTime").toString());
                    welfareOrderApiCardOpt.setExpireTime(expDate);
                }
                list.add(welfareOrderApiCardOpt);
            }
            welfareOrderApiCardService.insertList(list);
        }
        return null;
    }
}
